<?php 
// +----------------------------------------------------------------------
// | ThinkPHP                                                             
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.      
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>                                  
// +----------------------------------------------------------------------
// $Id$

// 标签管理类
class Tag extends Base {
    // 标签
    private static $_tag    =   array();

    /**
     +----------------------------------------------------------
     * ThinkPHP标签执行方法
     *  例如 Tag::add('init','app_init',5) 在init标签中添加app_init方法
     * 优先级别为5 0为最高级别
     +----------------------------------------------------------
     * @param string $tag 标签名称
     * @param string $callback 要执行的方法
     * @param Integer $priority 优先级别
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public static function add($tag,$callback,$priority=10) {
        // 记录某个标签要执行的方法
        self::$_tag[$tag][$priority][]  =   $callback;
        return ;
    }

    /**
     +----------------------------------------------------------
     * 使用指定的标签方法
     +----------------------------------------------------------
     * @param string $tag 标签名称
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public static function apply($tag) {
        if( isset(self::$_tag[$tag]) && is_array(self::$_tag[$tag])) {
            // 对标签方法按照优先级别排序
            ksort(self::$_tag[$tag]);
            // 支持方法的参数
            $args = array_slice(func_get_args(), 1);
            foreach (self::$_tag[$tag] as $priority=>$tags){
                foreach ($tags as $callback){
                    if(is_callable($callback)) $result   =   call_user_func_array($callback,$args);
                }
            }
            return $result;
        }
        return ;
    }

    /**
     +----------------------------------------------------------
     * 判断是否有给某个标签定义方法
     +----------------------------------------------------------
     * @param string $tag 标签名称
     +----------------------------------------------------------
     * @return boolean
     +----------------------------------------------------------
     */
    public static function exists($tag) {
        if(isset(self::$_tag[$tag])){
            return true;
        }else{
            return false;
        }
    }
}
?>